// acwing 837
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1e5 + 10;

int p[N], size[N];
int m, n;

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= 5; i++) {
        p[i] = i;
        size[i] = 1;
    }
    while (m -- ) {
        char op[4];
        scanf("%s", op);
        
        int a, b;
        if (op[0] == 'C') {
            scanf("%d%d", &a, &b);
            if (find(a) == find(b)) continue;
            size[find(b)] += size[find(a)];
            p[find(a)] = find(b);
        } else if (op[1] == '1') {
            scanf("%d%d", &a, &b);
            if (find(a) == find(b)) cout << "Yes" << endl;
            else cout << "No" << endl;
        } else {
            scanf("%d", &a);
            cout << size[find(a)] << endl;
        }
    }

    return 0;
}